package com.example.controller;

import com.example.ErrorEnum;
import com.example.annotation.IgnoreToken;
import com.example.annotation.Xuxin;
import com.example.base.BaseAction;
import com.example.base.DemoContext;
import com.example.base.DemoException;
import com.example.base.ResponseDto;
import com.example.module.Student;
import com.example.service.StudentService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


@RestController
@Xuxin
@RequestMapping("/student")
public class StudentController extends BaseAction {
    @Resource
    private StudentService studentService;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    public static void main(String[] args) {
        System.out.println(


        );
    }

    private final Logger log =LoggerFactory.getLogger(StudentController.class);

    @RequestMapping(value="/user/add",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    public ResponseDto<Integer> add111(@RequestBody Student student) throws Exception{
//        Thread.sleep(5000);
        String phone = DemoContext.getPhone();
        log.info("当前用户手机号是 {}",phone);
        String token = DemoContext.getToken();
        log.info("当前用户token是 {}",token);
        studentService.add(student);
        return super.sendSuccessMessage(1);
    }

    @IgnoreToken
    @RequestMapping(value="/user/testRedis",method = RequestMethod.GET,produces="application/json;charset=UTF-8")
    public ResponseDto<Integer> testRedis(@RequestBody Student student)throws Exception{
        if(!StringUtils.hasLength(student.getName())){
            throw new DemoException(ErrorEnum.LACK_PARAMETER.getCode(), ErrorEnum.LACK_PARAMETER.getMessage());
        }
        String phone = DemoContext.getPhone();
        log.info("当前用户手机号是 {}",phone);
        String token = DemoContext.getToken();
        log.info("当前用户token是 {}",token);
         redisTemplate.opsForValue().set("name",student.getName());
        System.out.println("redis对象 = " + redisTemplate.toString());
         return super.sendSuccessMessage(1);
    }


    @IgnoreToken
    @RequestMapping(value="/user/getRedis",method = RequestMethod.GET,produces="application/json;charset=UTF-8")
    public ResponseDto<String> getRedis()throws Exception{
//        if(!StringUtils.hasLength(student.getName())){
//            throw new DemoException(ErrorEnum.LACK_PARAMETER.getCode(), ErrorEnum.LACK_PARAMETER.getMessage());
//        }

        String name = redisTemplate.opsForValue().get("name");
        return super.sendSuccessMessage(Objects.requireNonNull(redisTemplate.opsForValue().get("name")));
    }

}
